﻿using System;
using System.Collections.Generic;
using System.Configuration.Provider;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;
using Telecom.Data;
using Telecom.Service;

namespace TelecomGis.Helper.MemberS
{
    public class VNPT_BSColeProvider : RoleProvider
    {
        private UserSer userService;
        private RoleSer Roluser;

        public VNPT_BSColeProvider()
        {
            userService = new UserSer();
            Roluser = new RoleSer();
        }

        private string applicationName = null;

        public override string ApplicationName
        {
            get
            {
                if (applicationName == null)
                {
                    applicationName = "Telecomgis";
                }
                return applicationName;
            }
            set { applicationName = value; }
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            foreach (string rolename in roleNames)
            {
                if (rolename == null || rolename == "")
                    throw new ProviderException("Role name cannot be empty or null.");
                if (!RoleExists(rolename))
                    throw new ProviderException("Role name not found.");
            }

            foreach (string username in usernames)
            {
                if (username == null || username == "")
                    throw new ProviderException("User name cannot be empty or null.");
                if (username.Contains(","))
                    throw new ArgumentException("User names cannot contain commas.");

                foreach (string rolename in roleNames)
                {
                    if (IsUserInRole(username, rolename))
                        throw new ProviderException("User is already in role.");
                }
            }
        }

        public override void CreateRole(string roleName)
        {
            ROLES roles = new ROLES();
            roles.TEN = roleName;
            roles.ROLEID = 0;
            Roluser.addOrupdate(roles);
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            Roluser.deleteByName(roleName);
            return true;
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            return Enum.GetValues(typeof(ROLES)).Cast<string>().ToArray();
        }

        public override string[] GetRolesForUser(string username)
        {
            string[] user = Roluser.GetRolesForUser(username);
            if (user != null)
            {
                return user;
            }
            return new string[] { string.Empty };
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            return Roluser.IsUserInRole(username, roleName);
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}
